package com.wms.interceptor;

import com.alibaba.fastjson.JSON;
import com.wms.config.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //用户重置登录 系统会给浏览器发送一个token
        // 2.浏览器接收到这个token 需要记录下来
        // 3.浏览器每次请求都会携带这个token
        // 4.在拦截器拦截这个请求
        String token = request.getHeader("token");
        if (token == null || token.isEmpty()) {
           Result result=Result.fail(401,"认证失败");
           //转成json
            String json=JSON.toJSONString(result);
            response.getWriter().write(json);
            return false;
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
